package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.constant.JiGuangConstant;
import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.OmcSchedulMonthMapper;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.constant.ScheduleConstant;
import com.csun.cmny.provider.model.domain.OmcPlan;
import com.csun.cmny.provider.model.domain.OmcSchedul;
import com.csun.cmny.provider.model.domain.OmcSchedulMonth;
import com.csun.cmny.provider.model.dto.SendMessageRequest;
import com.csun.cmny.provider.service.OmcPlanService;
import com.csun.cmny.provider.service.OmcSchedulMonthService;
import com.csun.cmny.provider.service.OpcMessageService;
import com.csun.cmny.provider.vo.OmcSchedulMonthVo;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Slf4j
@Service
public class OmcSchedulMonthServiceImpl extends BaseService<OmcSchedulMonth> implements OmcSchedulMonthService {
    @Resource
    private OmcSchedulMonthMapper omcSchedulMonthMapper;
    @Resource
    private OpcMessageService opcMessageService;
    @Resource
    private OmcPlanService omcPlanService;


    /**
     * 保存排班计划
     * @param omcSchedulMonth
     * @return Wrapper
     * @author HD
     * @date 2021/12/6 15:54
     */
    @Override
    public Integer saveSchedulMonth(OmcSchedulMonth omcSchedulMonth) {

        //校验该周计划是否存在
        OmcSchedulMonth schedulMonth = new OmcSchedulMonth();
        schedulMonth.setNursingGroupId(omcSchedulMonth.getNursingGroupId());
        schedulMonth.setMonth(omcSchedulMonth.getMonth());
        schedulMonth.setGroupId(omcSchedulMonth.getGroupId());
        schedulMonth.setId(omcSchedulMonth.getId());
        List<OmcSchedulMonth> omcSchedulQueryList = omcSchedulMonthMapper.select(schedulMonth);
        if(omcSchedulQueryList.size() > 0){
            delete(schedulMonth);
        }

        //生成推送
        SendMessageRequest sendMessageRequest = new SendMessageRequest();
        sendMessageRequest.setMsgCode(ScheduleConstant.Schedule_FLOW_NURSING_ADD);
        sendMessageRequest.setGroupId(omcSchedulMonth.getGroupId());
        //当计划不存在时,计划使用次数+1
        OmcPlan omcPlan = omcPlanService.selectByKey(omcSchedulMonth.getPlanId());
        omcPlan.setIsUsed(omcPlan.getIsUsed()+1);
        int ret = omcPlanService.update(omcPlan);
        if(ret < 1){
            throw new OmcBizException(ErrorCodeEnum.OMC10035005);
        }
        //批量添加计划
        int i = save(omcSchedulMonth);

        //推送
        opcMessageService.pushWithMsgCode(sendMessageRequest);
        return i;
    }

    /**
     * 排班计划(月)-管理员审批
     * @param: [schedulListDto, br]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @Override
    public Integer approvalScheduleMonth(OmcSchedulMonth omcSchedulMonth) {
        //查询该周交班项，检验审批状态
        OmcSchedulMonth schedulQuery = new OmcSchedulMonth();
        schedulQuery.setMonth(omcSchedulMonth.getMonth());
        schedulQuery.setNursingGroupId(omcSchedulMonth.getNursingGroupId());
        List<OmcSchedulMonth> currentSchedulList = select(schedulQuery);

        if(currentSchedulList.size() < 1){
            throw new OmcBizException(ErrorCodeEnum.OMC10036001);
        }

        int currentStatus = currentSchedulList.get(0).getStatus();
        log.info("========= [currentStatus] : " + currentStatus);

        if(currentStatus != 0){
            throw new OmcBizException(ErrorCodeEnum.OMC10036009);
        }

        //获取审批原因
        String reason = omcSchedulMonth.getReason();
        //获取审批状态 1.未通过 2.通过
        Integer status = omcSchedulMonth.getStatus();
        OmcSchedulMonth omcSchedul = new OmcSchedulMonth();
        omcSchedul.setStatus(omcSchedulMonth.getStatus());
        omcSchedul.setReason(omcSchedulMonth.getReason());
        omcSchedul.setId(omcSchedulMonth.getId());
        int i = omcSchedulMonthMapper.updateByPrimaryKeySelective(omcSchedul);
        if(currentSchedulList.size() > 0){
            //申请人Id
            Long applyId = selectByKey(currentSchedulList.get(0).getId()).getCreatorId();

            //生成推送
            SendMessageRequest sendMessageRequest = new SendMessageRequest();
            sendMessageRequest.setUserIdList(Lists.newArrayList(applyId));
            sendMessageRequest.setGroupId(currentSchedulList.get(0).getGroupId());
            /**
             * @date    2019-12-09
             * @author  chisj
             * @desc    推送描述信息
             */
            sendMessageRequest.setExtrasParam(currentSchedulList.get(0));
            sendMessageRequest.setTagList(Lists.newArrayList(JiGuangConstant.TAG_PREFIX_USER + applyId));
            if(status == 2){
                sendMessageRequest.setMsgCode(ScheduleConstant.Schedule_FLOW_SERVICE_AGREE);
            }else if(status == 1){
                sendMessageRequest.setMsgCode(ScheduleConstant.Schedule_FLOW_SERVICE_REFUSE);
            }
            opcMessageService.pushByUser(sendMessageRequest);
        }
        return i;
    }
    /*
    * 查询该天班组负责人
    * */
    @Override
    public List<OmcSchedulMonth> queryShiftUsers(OmcSchedul omcSchedul, LoginAuthDto loginAuthDto) {
        OmcSchedulMonth omcSchedulMonth = new OmcSchedulMonth();
        omcSchedulMonth.setNursingGroupId(omcSchedul.getNursingGroupId());
        omcSchedulMonth.setGroupId(loginAuthDto.getGroupId());
        omcSchedulMonth.setMonth(omcSchedul.getMonth());
        return omcSchedulMonthMapper.select(omcSchedulMonth);
    }
}
